<?php
if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

class plugin_pasteimage
{

}

class plugin_pasteimage_forum extends plugin_pasteimage
{
    public function post_middle()
    {
        global $_G;
        $this->config = $_G['cache']['plugin']['pasteimage'];
        if(!$this->config['on']){
            return false;
        }
        $this->config['groups'] = unserialize($this->config['groups']);
        $this->config['forums'] = unserialize($this->config['forums']);
        if(! in_array($_G['groupid'], $this->config['groups'])){
            return false;
        }
        if(!in_array($_G['forum']['fid'], $this->config['forums'])){
            return false;
        }

        $l_uploading   = lang('plugin/pasteimage', 'uploading');
        $l_cant_read   = lang('plugin/pasteimage', 'cant_read');
        $l_cant_upload = lang('plugin/pasteimage', 'cant_upload');
        $l_not_allow_upload = lang('plugin/pasteimage', 'not_allow_upload');
        $l_upload_succeed = lang('plugin/pasteimage', 'upload_succeed');
        $_l_i_know = lang('plugin/pasteimage', 'i_know');
        $fid = intval($_GET['fid']);

        $tip_ctrl = $tip_func = '';
        if($this->config['tip']){
            $tip_message = $this->config['tip_message'];
            $tip_ctrl = <<<SCRIPT
    if(getcookie('close_paste_notice_tip') != '1'){
        var paste_tip = document.createElement('div');
        paste_tip.setAttribute('class', 'tip tip_2');
        paste_tip.setAttribute('id', 'paste_tip');
        paste_tip.innerHTML  = '<div style="right:240px;" class="tip_horn"></div><div class="tip_c">' +
         '$tip_message &nbsp; <a href="javascript:;" class="xi2" onclick="return hide_paste_notice_tip(this);">$_l_i_know</a></div>';
        insertAfter(paste_tip, $('rstnotice'));
    }
SCRIPT;
            $tip_func = <<<FUNCTION
function hide_paste_notice_tip(event){
    setcookie('close_paste_notice_tip', 1, 3600);
    event.parentNode.parentNode.style.display = 'none';
    return false;
}
FUNCTION;
        }

        return "
<style type='text/css'>
.upload-loading,.upload-succeed{display: none;position:absolute;left:45%;width:150px;padding: 5px;text-align:center;background: rgb(202,142,80);color:white;font-weight:bold;border-radius: 0 0 3px 3px;}
.upload-succeed{background: #6DB227;}
#paste_tip{ margin:50px 0 0 20px;}
</style>
<script type=\"text/javascript\">
if(!isUndefined(FileReader))
{
    var yzg_upload_notice = '{$l_uploading}0/1...';
    if(!XMLHttpRequest.prototype.sendAsBinary){
        XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
            function byteValue(x) {
                return x.charCodeAt(0) & 0xff;
            }
            var ords = Array.prototype.map.call(datastr, byteValue);
            var ui8a = new Uint8Array(ords);
            this.send(ui8a.buffer);
        }
    }
$tip_ctrl
    setTimeout(function(){
        var sub_document = document.getElementById('e_iframe').contentWindow.document;
        sub_document.body.addEventListener('paste', function(e) {
            for (var i = 0; i < e.clipboardData.items.length; i++) {
                if (e.clipboardData.items[i].kind == 'file' &&
                        ( e.clipboardData.items[i].type == 'image/png' ||
                          e.clipboardData.items[i].type == 'image/gif' ||
                          e.clipboardData.items[i].type == 'image/jpeg' )
                    ) {
                    var imageFile = e.clipboardData.items[i].getAsFile();

                    if($('upload-loading')){
                        $('upload-loading').parentNode.removeChild($('upload-loading'));
                    }
                    var progressbar = document.createElement('div');
                    progressbar.setAttribute('class', 'upload-loading');
                    progressbar.setAttribute('id', 'upload-loading');
                    insertAfter(progressbar, $('rstnotice'));
                    uploadFile(imageFile, progressbar);
                    e.preventDefault();
                    break;
                }
            }
        });
    }, 200);
}
function uploadFile(file, progressbar) {
    var reader = new FileReader();
    reader.onloadend = function(){
        var url = 'plugin.php?id=pasteimage:upload&type='+file.type +'&fid=$fid';
        var xhr = new XMLHttpRequest();
        var upload = xhr.upload;

        var p = document.createElement('p');
        p.textContent = yzg_upload_notice + '0%';
        progressbar.appendChild(p);
        progressbar.style.display = 'block';
        if(file.size <=0){
            show_paste_tip('$l_cant_read', progressbar);
            return false;
        }

        upload.progressbar = progressbar;
        upload.addEventListener('progress', yzg_uploadProgress, false);
        upload.addEventListener('load', yzg_uploadSucceed, false);
        upload.addEventListener('error', yzg_uploadError, false);

        xhr.open('POST', url);
        xhr.overrideMimeType('application/octet-stream');
        xhr.sendAsBinary(this.result);
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4 && xhr.status == 200){
                var response = xhr.responseText;
                if(response != '-1' && response!='' && response!='-2'){
                    response = (new Function('', 'return ' + response))();
                    var sub_document = document.getElementById('e_iframe').contentWindow.document;
                    var input = sub_document.createElement('input');
                    input.type = 'hidden';
                    input.name = 'attachnew['+response.aid+'][description]';
                    document.getElementById('postform').appendChild(input);
                    insertText('<img src=\"' + response.src + '\" border=\"0\" aid=\"attachimg_' + response.aid + '\" alt=\"\" />', false);
                }else if(response =='-2'){
                    show_paste_tip('$l_not_allow_upload', progressbar);
                }else{
                    show_paste_tip('$l_cant_upload', progressbar);
                }
            }
        }
    }
    reader.readAsBinaryString(file);
}
function yzg_uploadProgress(event){
    if (event.lengthComputable) {
        var percentage = Math.round((event.loaded * 100) / event.total);
        if (percentage < 100) {
            event.target.progressbar.firstChild.textContent = yzg_upload_notice + percentage + '%';
        }
    }
}
function yzg_uploadSucceed(event) {
    event.target.progressbar.className = 'upload-succeed';
    event.target.progressbar.firstChild.textContent = '$l_upload_succeed';
    var s_progressbar = event.target.progressbar;
    setTimeout(function(){
        s_progressbar.style.display = 'none';
    }, 750);
}
function yzg_uploadError(error) {
    alert('error: ' + error);
}
function insertAfter(newEl, targetEl){
    var parentEl = targetEl.parentNode;
    if(parentEl.lastChild == targetEl){
        parentEl.appendChild(newEl);
    }else{
        parentEl.insertBefore(newEl,targetEl.nextSibling);
    }
}
function show_paste_tip(tip, progressbar){
    progressbar.firstChild.textContent = tip;
    setTimeout(function(){
        progressbar.style.display = 'none';
    }, 750);
}
$tip_func
</script>";
    }
}